//evlumte the XMLHttpRequest constractor in IE
function AjaxHttpRequest(){
	if( window.XMLHttpRequest ){
		return new XMLHttpRequest();
	}else if( window.ActiveXObject ){
		return new ActiveXObject( "Microsoft.XMLHTTP" );
	}
}

/**
 * 该函数可以为任意需要用异步来实现功能的区域
 * 其只需重写 callback() 函数。
 * @returns
 */
function ajax( url, data, callback ){
	if( arguments.length < 2 ){
		alert( 'arguments length is less than 2' );
		return;
	}
	//若参数长度小于 3, 则说明 data 为nul
	if( arguments.length < 3 ){
		var callback = arguments[1];
		if( typeof callback !=='function' ){
			alert( 'callback is not a function' );
			return;
		}
		var data = null;
	}
	
	var request = AjaxHttpRequest();
	//以 post 方式发送请求
	request.open( "POST", url, true );
	//处理返回结果
	request.onreadystatechange = function(){
		if( request.readyState === 4 && request.status === 200 ){
			var type = request.getResponseHeader( "Content-Type" );
			//接受返回的内容并将其传递给回调函数
			if( type.match( /^text/) ){
				callback( request );
			}
		}
	}
	//发送数据
	request.send( data );
}